﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Mokey.Common;
using Mokey.Common.Forms;

namespace Mokey.ThunderCards.Cards
{
    public partial class AddEditCardDialog : Form
    {
        public AddEditCardDialog()
        {
            InitializeComponent();
        }

        private ThunderCard _card;
        public ThunderCard Card
        {
            get
            {
                if (_card == null)
                {
                    _card = new ThunderCard();
                }
                return _card;
            }
            set
            {
                _card = value;
                QuestionTextBox.Text = _card.Question;
                AnswerTextBox.Text = _card.Answer;
                QuestionImageTextBox.Text = _card.QuestionImageFileName;
                AnswerImageTextBox.Text = _card.AnswerImageFileName;
                SoundTextBox.Text = _card.SoundFileName;

                if (QuestionImageTextBox.Text.Trim() != string.Empty)
                {
                    QuestionImageRadioButton.Checked = true;
                }
                else
                {
                    QuestionRadioButton.Checked = true;
                }

                if (AnswerImageTextBox.Text.Trim() != string.Empty)
                {
                    AnswerImageRadioButton.Checked = true;
                }
                else
                {
                    AnswerRadioButton.Checked = true;
                }
            }
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            if (ValidateFiles())
            {
                Card.Question = QuestionTextBox.Text;
                Card.Answer = AnswerTextBox.Text;
                Card.QuestionImageFileName = QuestionImageTextBox.Text.Trim();
                Card.AnswerImageFileName = AnswerImageTextBox.Text.Trim();
                Card.SoundFileName = SoundTextBox.Text.Trim();
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void SoundBrowseButton_Click(object sender, EventArgs e)
        {
            if (OpenImageDialog.ShowDialog() == DialogResult.OK)
            {
                SoundTextBox.Text = OpenImageDialog.FileName;
            }
        }

        private void AnswerImageBrowseButton_Click(object sender, EventArgs e)
        {
            if (OpenImageDialog.ShowDialog() == DialogResult.OK)
            {
                AnswerImageTextBox.Text = OpenImageDialog.FileName;
            }
        }

        private void QuestionImageBrowseButton_Click(object sender, EventArgs e)
        {
            if (OpenImageDialog.ShowDialog() == DialogResult.OK)
            {
                QuestionImageTextBox.Text = OpenImageDialog.FileName;
            }
        }

        private bool ValidateFiles()
        {
            bool isValid = true;
            //only validate the files if they've changed names
            if (QuestionImageTextBox.Text != Card.QuestionImageFileName && !QuestionImageTextBox.Text.IsTrimNullOrEmpty())
            {
                //make sure "isValid" comes second in the "&&". We validate all files to get validation messages, even if valid is already false
                isValid = (ValidationHelper.ValidateFileExists(QuestionImageTextBox, QuestionImageRadioButton) && isValid);
            }
            if (AnswerImageTextBox.Text != Card.AnswerImageFileName && !AnswerImageTextBox.Text.IsTrimNullOrEmpty())
            {
                isValid = (ValidationHelper.ValidateFileExists(AnswerImageTextBox, AnswerImageRadioButton) && isValid);
            }
            if (SoundTextBox.Text != Card.SoundFileName && !SoundTextBox.Text.IsTrimNullOrEmpty())
            {
                isValid = (ValidationHelper.ValidateFileExists(SoundTextBox, SoundLabel) && isValid);
            }

            string s = null;
            s.IsTrimNullOrEmpty();

            return isValid;
        }

        private void QuestionRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            QuestionTextBox.Enabled = QuestionRadioButton.Checked;
            QuestionImageTextBox.Enabled = QuestionImageRadioButton.Checked;
        }

        private void AnswerRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            AnswerTextBox.Enabled = AnswerRadioButton.Checked;
            AnswerImageTextBox.Enabled = AnswerImageRadioButton.Checked;
        }
    }
}
